package com.travelplanning.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.travelplanning.admin.common.convention.Result;
import com.travelplanning.admin.common.convention.Results;
import com.travelplanning.admin.dto.req.UserLoginReqDTO;
import com.travelplanning.admin.dto.req.UserRegisterReqDTO;
import com.travelplanning.admin.dto.req.UserUpdateReqDTO;
import com.travelplanning.admin.dto.resp.UserActualRespDTO;
import com.travelplanning.admin.dto.resp.UserLoginRespDTO;
import com.travelplanning.admin.dto.resp.UserRespDTO;
import com.travelplanning.admin.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户相关接口")
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查找信息
     */
    @Operation(summary = "根据用户名查找信息")
    @GetMapping("/api/travel-planning/v1/user")
    public Result<UserRespDTO> getUserByUsername(){
        return Results.success(userService.getUserByUsername());
    }
//    @Operation(summary = "根据用户名查找真实信息")
//    @GetMapping("/api/travel-planning/admin/v1/actual/user/{username}")
//    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
//        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
//    }

    /**
     * 查询用户名是否存在
     */
    @Operation(summary = "查询用户名是否存在")
    @GetMapping("/api/travel-planning/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     *用户注册
     */
    @Operation(summary = "用户注册")
    @PostMapping("/api/travel-planning/v1/user/register")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestparam){
        userService.register(requestparam);
        return Results.success();
    }

    /**
     * 修改用户信息
     */
    @Operation(summary = "修改用户信息")
    @PutMapping("/api/travel-planning/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestparam){
        userService.update(requestparam);
        return Results.success();
    }

    /**
     *用户登录
     */
    @Operation(summary = "用户登录")
    @PostMapping(value = "/api/travel-planning/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestparam){
        return Results.success(userService.login(requestparam));
    }
    /**
     * 检查用户是否登录
     */
    @Operation(summary = "检查用户是否登录")
    @GetMapping("/api/travel-planning/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出登录
     */
    @Operation(summary = "用户退出登录")
    @DeleteMapping ("/api/travel-planning/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }



}
